package apresentacao.caminhoneiro;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import tcc.caminhoneiro.R;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

public class CopyOfFragment_Meu_Perfil_GPS extends Fragment {

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_meu_perfil, null);
		inicializar(view);
		Toast.makeText(getActivity(), "Meu Perfil", Toast.LENGTH_SHORT).show();
		return view;
	}

	private void inicializar(View view) {
		LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
		LocationListener list = new LocationListener() {
			
			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), "a3", Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), "a2", Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), "a1", Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub				
				try {
					Toast.makeText(getActivity(), location.getLatitude()+" "+location.getLongitude(), Toast.LENGTH_SHORT).show();
					Geocoder geo = new Geocoder(getActivity(), Locale.getDefault());
					List<Address> end = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
					if(end.size() >= 1){
						String a = end.get(0).getAdminArea();
						String b = end.get(0).getCountryName();
						String c = end.get(0).getPostalCode();
						Toast.makeText(getActivity(), a+" "+b+" "+c, Toast.LENGTH_SHORT).show();	
					}else{
						Toast.makeText(getActivity(), "erro", Toast.LENGTH_SHORT).show();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, (long) (0.5 * 60 * 1000) , 0, list);
	}
}
